<?php
/**
 *  普通订单
*/

namespace app\api\model;
use think\facade\Lang;


class Order extends BaseModel
{
    protected $append = ['status_text','pay_status_text','order_type_text','delivery_method_text','pay_terms_text'];
    protected $attributes = [
        'order_type' => [
            '1' => 'orderType.userOrders',
            '2' => 'orderType.dealerShipmentOrder',
            '3' => 'orderType.saleOrder',
            '4' => 'orderType.splitOrder',
            '5' => 'orderType.mergeOrders'
        ],
        'delivery_method' => [
            0 => 'deliveryMethod.selfPickup',
            1 => 'deliveryMethod.expressDelivery',
            2 => 'deliveryMethod.destinationWarehouse'
        ],

        'delivery_terms' => [
            1 => 'deliveryTerms.ddp',
            2 => 'deliveryTerms.ddu'
        ],

        'prescription' => [
            1 => 'prescription.generalExpress',
            2 => 'prescription.express'
        ],

        'pay_terms' => [
            0 => 'payTerms.fullPayment',
            1 => 'payTerms.advanceCharge'
        ],
        'pay_status' => [
            1 => 'payStatus.unpaid',
            2 => 'payStatus.audit',
            3 => 'payStatus.paid',
            4 => 'payStatus.partialPayment',
        ],
        'status' => [
            10 => 'status.pendingPayment',  // 待支付
            20 => 'status.inReview', // 待审核
            21 => 'status.pendingPayment',   // 待支付
            30 => 'status.pendingShipment',  // 代发货
            40 => 'status.partialShipment',  // 部分收货
            50 => 'status.pendingReceipt',  // 待收货
            70 => 'status.cancelled',  // 已取消
            100 => 'status.completed',  // 已完成
        ],
        'pay_method' => [
            1 => 'payMethod.cryptoPay',
            2 => 'payMethod.coinbasePay',
            3 => 'payMethod.wireTransfer',
        ]
    ];

    /**
     *  订单状态
    */
    public function getStatusTextAttr($value,$data)
    {
        $statusList = $this->attributes;
        if (isset($statusList['status'][$data['status']])){
            return Lang::get($statusList['status'][$data['status']]);
        } else {
            return Lang::get('order.orderStatusError');
        }
    }


    /**
     *  支付状态
    */
    public function getPayStatusTextAttr($value,$data)
    {
        $statusList = $this->attributes;
        if (isset($statusList['pay_status'][$data['pay_status']])){
            return Lang::get($statusList['pay_status'][$data['pay_status']]);
        } else {
            return Lang::get('order.orderStatusError');
        }
    }


    /**
     *  是否自提
     */
    public function getDeliveryMethodTextAttr($value, $data)
    {
        $statusList = $this->attributes;

        // 防御性编程：检查delivery_method键是否存在
        if (!isset($statusList['delivery_method'])) {
            return Lang::get('order.orderStatusError');
        }

        // 检查delivery_method字段是否存在于数据中
        if (!isset($data['delivery_method'])) {
            return Lang::get('order.orderStatusError');
        }

        if (isset($statusList['delivery_method'][$data['delivery_method']])) {
            return Lang::get($statusList['delivery_method'][$data['delivery_method']]);
        } else {
            return Lang::get('order.orderStatusError');
        }
    }

    /**
     *  是否全款
    */
    public function getPayTermsTextAttr($value,$data)
    {
        $statusList = $this->attributes;
        if (isset($statusList['pay_terms'][$data['pay_terms']])){
            return Lang::get($statusList['pay_terms'][$data['pay_terms']]);
        } else {
            return Lang::get('order.orderStatusError');
        }
    }

    /**
     *
     *  order_type_text
    */
    public function getOrderTypeTextAttr($value,$data)
    {

        $statusList = $this->attributes;
        if (isset($statusList['order_type'][$data['order_type']])){
            return Lang::get($statusList['order_type'][$data['order_type']]);
        } else {
            return Lang::get('order.orderStatusError');
        }
    }



    /**
     *  关联订单明细
    */
    public function item()
    {
        return $this->hasMany(OrderItem::class,'order_id','id')->where('is_del', 0);
    }

    /**
     *  过去订单状态
    */
    public function getFieldEnum($field)
    {
        if ('ALL' === strtoupper($field)) {
            return [
                'order_type' => $this->attributes['order_type'],
                'delivery_method' => $this->attributes['delivery_method'],
                'pay_terms' => $this->attributes['pay_terms'],
                'pay_status' => $this->attributes['pay_status'],
                'status' => $this->attributes['status'],
                'pay_method' => $this->attributes['pay_method']
            ];
        } else {
            return [$field => $this->$field];
        }
    }

    /**
     *  getAttributes
    */
    public function getAttributes($field)
    {
        $Attributes = $this->attributes;
        if(!isset($Attributes[$field])) {
            return [];
        }
        $label = [];
        foreach ($Attributes[$field] as $key => $value) {
            $label[] = [
                'label' =>  Lang::get($value),
                'value' => $key
            ];
        }
        return $label;
    }


    /**
     *  total_amount
     */
    public function getTotalAmountAttr($value)
    {
        if(is_null($value)){
            return '';
        } else {
            return $value;
        }
    }


    /**
     *  tax_fee
     */
    public function getTaxFeeAttr($value)
    {
        if(is_null($value)){
            return '';
        } else {
            return $value;
        }
    }

    /**
     *  logistics_fee
     */
    public function getLogisticsFeeAttr($value)
    {
        if(is_null($value)){
            return '';
        } else {
            return $value;
        }
    }

    /**
     *  order_pay_amount
     */
    public function getOrderPayAmountAttr($value)
    {
        if(is_null($value)){
            return '';
        } else {
            return $value;
        }
    }

    /**
     *  支付记录
    */
    public function paymentRecord()
    {
        return $this->hasMany(OrderPayment::class,'order_id','id');
    }

}
